package com.xmb.wechat.delegate;

import android.app.Activity;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.nil.sdk.utils.DateUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.baserecyclerview.BaseRecyclerViewManager;
import com.xmb.wechat.common.Constant;
import com.xmb.wechat.bean.TradeHistoryBean;
import com.xmb.wechat.util.MoneyFormat;
import com.xmb.wechat.util.PicLoadUtils;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/23 17:07
 */
public class TradeHistoryDetailDelegate extends BaseRecyclerViewManager {

    public TradeHistoryDetailDelegate(Activity activity) {
        super(activity);
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        return new TradeHistoryDetailAdapter(new ArrayList<>());
    }

    class TradeHistoryDetailAdapter extends BaseMultiItemQuickAdapter<TradeHistoryBean, BaseViewHolder> {

        /**
         * Same as QuickAdapter#QuickAdapter(Context,int) but with
         * some initialization data.
         *
         * @param data A new list is created out of this one to avoid mutable list
         */
        TradeHistoryDetailAdapter(List<TradeHistoryBean> data) {
            super(data);
            addItemType(0, R.layout.item_trade_history_detail_time);
            addItemType(Constant.TradeHistoryType.TYPE_RED_PACKET, R.layout.item_trade_history_detail_common);
            addItemType(Constant.TradeHistoryType.TYPE_CHANGE, R.layout.item_trade_history_detail_common);
            addItemType(Constant.TradeHistoryType.TYPE_QRCODE, R.layout.item_trade_history_detail_common);
            addItemType(Constant.TradeHistoryType.TYPE_ZHUANZHANG, R.layout.item_trade_history_detail_common);
            addItemType(Constant.TradeHistoryType.TYPE_ZIDINGYI, R.layout.item_trade_history_detail_common);
        }

        @Override
        protected void convert(BaseViewHolder helper, TradeHistoryBean item) {
            if (item.getType() == 0) {//时间
                helper.setText(R.id.tv_time, TextUtils.isEmpty(item.getMonthInYear()) ? "" : item.getMonthInYear());
                helper.setText(R.id.tv_count, TextUtils.isEmpty(item.getTotal()) ? "" : item.getTotal());
            } else {//通用
                ImageView imageView = helper.getView(R.id.iv_icon);
                Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/WeChatLqNumber.ttf");
                TextView tvMoney = helper.getView(R.id.tv_money);
                if (tf != null)
                    tvMoney.setTypeface(tf);
                switch (item.getType()) {
                    case Constant.TradeHistoryType.TYPE_ZIDINGYI://自定义
                    case Constant.TradeHistoryType.TYPE_ZHUANZHANG://转账
                        PicLoadUtils.setupRoundPicIntoImageView(item.getIconSourceType(), item.getIconUrl(), imageView);
                        //money
                        showMoneyWithDifferentColor(helper, item);
                        break;
                    case Constant.TradeHistoryType.TYPE_RED_PACKET://红包
                        //                        if (item.isExpend()) {
                        PicLoadUtils.setupRoundPicIntoImageView(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE,
                                String.valueOf(R.drawable.icon_trade_history_hb), imageView);
                        //                        } else {
                        //                            PicLoadUtils.setupRoundPicIntoImageView(item.getIconSourceType(), item.getIconUrl(), imageView);
                        //                        }
                        //money
                        showMoneyWithDifferentColor(helper, item);
                        break;
                    case Constant.TradeHistoryType.TYPE_QRCODE://二维码
                        //avatar
                        if (item.isExpend()) {
                            PicLoadUtils.setupRoundPicIntoImageView(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE,
                                    String.valueOf(R.drawable.icon_trade_history_ewm), imageView);
                        } else {
                            PicLoadUtils.setupRoundPicIntoImageView(item.getIconSourceType(), item.getIconUrl(), imageView);
                        }
                        //money
                        showMoneyWithDifferentColor(helper, item);
                        break;
                    case Constant.TradeHistoryType.TYPE_CHANGE://零钱
                        PicLoadUtils.setupRoundPicIntoImageView(Constant.SourceType.AVATAR_SRC_APP_INNER_DRAWABLE,
                                String.valueOf(R.drawable.icon_trade_history_lq), imageView);
                        //money
                        showMoneyWithSameColor(helper, item);
                        break;
                }
                helper.setText(R.id.tv_type, TextUtils.isEmpty(item.getContent()) ? "" : item.getContent());
                helper.setText(R.id.tv_time, DateUtils.getStringByFormat(new Date(item.getTradeTime()), "yyyy-MM-dd HH:mm"));
            }
        }
    }

    /**
     * 收入与支出不一样颜色
     *
     * @param helper
     * @param item
     */
    private void showMoneyWithDifferentColor(BaseViewHolder helper, TradeHistoryBean item) {
        if (item.isExpend()) {
            helper.setTextColor(R.id.tv_money, context.getResources().getColor(R.color.black));
            helper.setText(R.id.tv_money, "-" + MoneyFormat.formatMoney(item.getMoney()));
        } else {
            helper.setTextColor(R.id.tv_money, context.getResources().getColor(R.color.yellow_80));
            helper.setText(R.id.tv_money, "+" + MoneyFormat.formatMoney(item.getMoney()));
        }
    }

    /**
     * 收入与支出颜色一样
     *
     * @param helper
     * @param item
     */
    private void showMoneyWithSameColor(BaseViewHolder helper, TradeHistoryBean item) {
        helper.setTextColor(R.id.tv_money, context.getResources().getColor(R.color.black));
        helper.setText(R.id.tv_money, MoneyFormat.formatMoney(item.getMoney()));
    }

    @Override
    protected RecyclerView.ItemDecoration getItenDecoration() {
        return new HorizontalDividerItemDecoration.Builder(context).size(SizeUtils.dp2px(8)).color(ContextCompat.getColor(context, R.color.transparent)).build();
    }
}
